#!/bin/sh
CUR_PATH=`dirname $(readlink -f $0)`
InstallPath=/opt/svc-sample
InstallDir=`dirname "${InstallPath}"`
SVC_TYPE=sysv

export PATH=/bin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

if [ -e "${InstallPath}" ];then
    echo "Install svc-sample failed! reason: svc-sample has been installed."
    exit 17
fi

if [ ! -z "$1" ]; then
    SVC_TYPE=$1
fi

#copy files
cp -rf "${CUR_PATH}/svc-sample" "${InstallDir}"
chmod +x "${InstallPath}/svc"

#install service
if [ "sysv" = "${SVC_TYPE}" ]; then
    cp -f "${CUR_PATH}/svc-sampled" "/etc/init.d/"
    chmod +x "/etc/init.d/svc-sampled"
    if [ -e /etc/redhat-release ]; then
        chkconfig --add svc-sampled
        chkconfig svc-sampled on
    else
        update-rc.d svc-sampled defaults
    fi
    service svc-sampled start
else
    cp -f "${CUR_PATH}/svc-sample.service" "/etc/systemd/system/"
    systemctl daemon-reload
    systemctl enable svc-sample.service
    systemctl start svc-sample
fi

echo "Install svc-sample success!"

exit 0
